// pages/member/home/home.js
var api = require('../../../utils/httpUtil.js');
const app = getApp();
Component({
  options: {
    addGlobalClass: true,
  },
  data: {
    iconList: [{
        id: 1,
        icon: 'newsfill',
        color: 'orange',
        badge: 0,
        name: '会议预定',
        url: '/pages/meeting/apply/apply'
      }, {
        id: 2,
        icon: 'repairfill',
        color: 'yellow',
        badge: 0,
        name: '报事报修',
        url: '/pages/matter/apply/apply'
      }, {
        id: 3,
        icon: 'timefill',
        color: 'olive',
        badge: 0,
        name: '历史访客',
        url: '/pages/visitor/history/history'
      }, {
        id: 4,
        icon: 'locationfill',
        color: 'cyan',
        badge: 0,
        name: '导航',
        url: '/pages/navigate/navigate'
      }, {
        id: 5,
        icon: 'group_fill',
        color: 'blue',
        badge: 0,
        name: '访客审批',
        url: '/pages/visitor/approve/approve'
      }, {
        id: 6,
        icon: 'qr_code',
        color: 'green',
        badge: 0,
        name: '临时通行',
        url: '/pages/member/qrcode/qrcode'
      },
      {
        id: 7,
        icon: 'formfill',
        color: 'olive',
        badge: 0,
        name: '会议管理',
        url: '/pages/meeting/center/center'
      }, {
        id: 8,
        icon: 'countdownfill',
        color: 'green',
        badge: 0,
        name: '环境监测',
        url: '/pages/env-monitor/env-monitor'
      }, {
        id: 9,
        icon: 'brandfill',
        color: 'red',
        badge: 0,
        name: '满意度调查',
        url: '/pages/survey/apply/apply'
      }, {
        id: 10,
        icon: 'unlock',
        color: 'orange',
        badge: 0,
        name: '远程开门',
        url: '/pages/unlock/unlock'
      }
    ],
    gridCol: 4,
    skin: false,
    cardCur: 0,
    slideShow: [],
    houseImages: [],
    queryData: {},
    menuCount: 12,
    userInfo: app.globalData.userInfo
  },
  lifetimes: {
    // 组件生命周期函数，在组件布局完成后执行，此时可以获取节点信息
    ready: function () {
      var menuList = this.data.iconList;
      if (!this.data.userInfo.surveyEnable) {
        menuList = menuList.filter(item => item.id !== 9);
      }
      // if (this.data.userInfo.type != 5) {
      //   menuList = menuList.filter(item => item.id !== 10);
      // }
      this.setData({
        iconList: menuList
      })
      this.loadAnnounce();
      this.loadAdv();
      this.loadLetter();
    }
  },
  methods: {
    coopClick(e) {
      wx.navigateTo({
        url: '/pages/about/about',
      })
    },
    newsClick: function (e) {
      var id = e.currentTarget.dataset.id;
      app.globalData.news = this.data.newsList[id];
      wx.navigateTo({
        url: '/pages/news/news'
      })
    },
    letterClick: function (e) {
      var id = e.currentTarget.dataset.id;
      app.globalData.newsletter = this.data.houseImages[id];
      wx.navigateTo({
        url: '/pages/newsletter/newsletter'
      })
    },
    //获取公司资讯
    loadAnnounce: function (e) {
      api.httpRequest(api.announceAPI, this.data.queryData)
        .then(res => {
          if (res.code == 200) {
            this.setData({
              newsList: res.data
            })
          } else {
            wx.showToast({
              title: res.msg,
              duration: 2000,
              mask: true,
              icon: 'none'
            })
          }

        })
        .catch(error => {
          wx.showToast({
            title: error.message,
            duration: 1000,
            mask: true,
            icon: 'none'
          })
        });
    },

    //获取广告
    loadAdv: function (e) {
      api.httpRequest(api.advAPI, this.data.queryData)
        .then(res => {
          if (res.code == 200) {
            this.setData({
              slideShow: res.data.slideshow
            })
          } else {
            wx.showToast({
              title: res.msg,
              duration: 2000,
              mask: true,
              icon: 'none'
            })
          }

        })
        .catch(error => {
          wx.showToast({
            title: error.message,
            duration: 1000,
            mask: true,
            icon: 'none'
          })
        });
    },
    //获取活动
    loadLetter: function (e) {
      api.httpRequest(api.newsletterAPI, this.data.queryData)
        .then(res => {
          if (res.code == 200) {
            this.setData({
              houseImages: res.data
            })
          } else {
            wx.showToast({
              title: res.msg,
              duration: 2000,
              mask: true,
              icon: 'none'
            })
          }

        })
        .catch(error => {
          wx.showToast({
            title: error.message,
            duration: 1000,
            mask: true,
            icon: 'none'
          })
        });
    },
  }
})